﻿using System.Collections.Generic;
using Chess.Common.Game;
using NUnit.Framework;
using System.Linq;

namespace Common.Test
{
    [TestFixture]
    public sealed class Game
    {
        [Test]
        public void PossibleMovesTest()
        {
            List<Coordinate> moves = new List<Coordinate>();

            for (char file = 'B'; file <= 'H'; file++)
            {
                moves.Add(new Coordinate(file, '1'));
            }
            for (char rank = '2'; rank <= '8'; rank++)
            {
                moves.Add(new Coordinate('A', rank));
            }

            Assert.IsTrue((
            PieceType.Rook.GetPossibleMoves("A1").SequenceEqual(moves)));
        }

        [Test]
        public void BoardPositionTest()
        {
            Board board = new Board();
            Dictionary<Coordinate, Piece> pieceMap = new Dictionary<Coordinate, Piece>();

            pieceMap["A1"] = new Piece(PieceType.Rook, Color.Black);
            pieceMap["B1"] = new Piece(PieceType.Knight, Color.Black);
            pieceMap["C1"] = new Piece(PieceType.Bishop, Color.Black);
            pieceMap["D1"] = new Piece(PieceType.Queen, Color.Black);
            pieceMap["E1"] = new Piece(PieceType.King, Color.Black);
            pieceMap["F1"] = new Piece(PieceType.Bishop, Color.Black);
            pieceMap["G1"] = new Piece(PieceType.Knight, Color.Black);
            pieceMap["H1"] = new Piece(PieceType.Rook, Color.Black);

            pieceMap["A2"] = new Piece(PieceType.Pawn, Color.Black);
            pieceMap["B2"] = new Piece(PieceType.Pawn, Color.Black);
            pieceMap["C2"] = new Piece(PieceType.Pawn, Color.Black);
            pieceMap["D2"] = new Piece(PieceType.Pawn, Color.Black);
            pieceMap["E2"] = new Piece(PieceType.Pawn, Color.Black);
            pieceMap["F2"] = new Piece(PieceType.Pawn, Color.Black);
            pieceMap["G2"] = new Piece(PieceType.Pawn, Color.Black);
            pieceMap["H2"] = new Piece(PieceType.Pawn, Color.Black);
            pieceMap["A1"] = new Piece(PieceType.Rook, Color.Black);

            pieceMap["B8"] = new Piece(PieceType.Knight, Color.White);
            pieceMap["C8"] = new Piece(PieceType.Bishop, Color.White);
            pieceMap["D8"] = new Piece(PieceType.Queen, Color.White);
            pieceMap["E8"] = new Piece(PieceType.King, Color.White);
            pieceMap["F8"] = new Piece(PieceType.Bishop, Color.White);
            pieceMap["G8"] = new Piece(PieceType.Knight, Color.White);
            pieceMap["H8"] = new Piece(PieceType.Rook, Color.White);

            pieceMap["A7"] = new Piece(PieceType.Pawn, Color.White);
            pieceMap["B7"] = new Piece(PieceType.Pawn, Color.White);
            pieceMap["C7"] = new Piece(PieceType.Pawn, Color.White);
            pieceMap["D7"] = new Piece(PieceType.Pawn, Color.White);
            pieceMap["E7"] = new Piece(PieceType.Pawn, Color.White);
            pieceMap["F7"] = new Piece(PieceType.Pawn, Color.White);
            pieceMap["G7"] = new Piece(PieceType.Pawn, Color.White);
            pieceMap["H7"] = new Piece(PieceType.Pawn, Color.White);

            Assert.IsTrue(pieceMap.Keys.All(coordinate => IsPiece(board, coordinate, pieceMap[coordinate])));
        }

        private bool IsPiece(Board board, Coordinate coordinate, Piece piece)
        {
            Piece boardPiece = board.GetPiece(coordinate);
            if (boardPiece == null) return false;

            return boardPiece.PieceType == piece.PieceType
                && boardPiece.Color == piece.Color;
        }
    }
}
